package com.yuexun.dhdoctor.module.login.ui;

import android.content.Intent;
import android.content.pm.PackageManager;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.login.bean.LoginBean;
import com.yuexun.dhdoctor.module.login.bean.UserBean;
import com.yuexun.dhdoctor.module.login.model.impl.LoginModel;
import com.yuexun.dhdoctor.module.login.model.impl.MenuPhoneModel;
import com.yuexun.dhdoctor.module.login.presenter.IMenuPhonePresenter;
import com.yuexun.dhdoctor.module.login.presenter.impl.LoginPresneter;
import com.yuexun.dhdoctor.module.login.presenter.impl.MenuPhonePresenter;
import com.yuexun.dhdoctor.module.login.ui.view.ILoginView;
import com.yuexun.dhdoctor.module.login.ui.view.IMenuPhoneView;
import com.yuexun.dhdoctor.module.main.bean.UpdateBean;
import com.yuexun.dhdoctor.module.main.model.impl.UpdateModel;
import com.yuexun.dhdoctor.module.main.presenter.IUpdatePresenter;
import com.yuexun.dhdoctor.module.main.presenter.impl.UpdatePresenter;
import com.yuexun.dhdoctor.module.main.ui.Act_Main;
import com.yuexun.dhdoctor.module.main.ui.view.IUpdateView;
import com.yuexun.dhdoctor.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by work-jx on 2016/6/30.
 */
public class Act_Start extends KJActivity implements ILoginView, IUpdateView, IMenuPhoneView {
    LoadingDialog dialog;
    private LoginPresneter presenter;
    IUpdatePresenter presenter_update;
    IMenuPhonePresenter phonePresenter;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_start);
    }

    @Override
    public void initData() {
        super.initData();
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        presenter_update = new UpdatePresenter(new UpdateModel(), this);
//        presenter_update.update();
        phonePresenter = new MenuPhonePresenter(new MenuPhoneModel(), this);
        showActivity(aty, Act_Login.class);
        finish();

    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;

            case AppConfig.DISMISS_DIALOG:
                dialog.dismiss();
                break;

        }
    }

    @Override
    public void doInLogin(LoginBean loginBean) {
        if (loginBean.getStatus() == 200) {
            String filename = AppConfig.USER_INFO;
            PreferenceHelper.write(aty, filename, AppConfig.JOB_ID, loginBean.getDatas().get(0).getJobNum());
            PreferenceHelper.write(aty, filename, AppConfig.SESSION_ID, loginBean.getDatas().get(0).getSessionUuid());
            PreferenceHelper.write(aty, filename, AppConfig.ROLETYPE, loginBean.getDatas().get(0).getRoleType());
            PreferenceHelper.write(aty, filename, AppConfig.DEPARTMENTID, loginBean.getDatas().get(0).getDepartmentId());
            PreferenceHelper.write(aty, filename, AppConfig.DEPARTMENTNAME, loginBean.getDatas().get(0).getDepartmentName());
            PreferenceHelper.write(aty, filename, AppConfig.ACCOUNT_ID, loginBean.getDatas().get(0).getAccountId());

            PreferenceHelper.write(aty, filename, AppConfig.USER_ID, loginBean.getDatas().get(0).getAccountId());
            PreferenceHelper.write(aty, filename, AppConfig.USER_NAME, loginBean.getDatas().get(0).getFullName());
            PreferenceHelper.write(aty, filename, AppConfig.USER_DEPT, loginBean.getDatas().get(0).getDepartmentName());

            Map<String, String> params = new HashMap<>();
            params.put("sessionUuid", loginBean.getDatas().get(0).getSessionUuid());
            presenter.getUseInfo(params);

//            Map<String, String> params = new HashMap<>();
//            params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
//            phonePresenter.getMenuPhone(params);
        } else {
            showActivity(aty, Act_Login.class);
            finish();
        }
    }

    @Override
    public void getUserInfo(UserBean userBean) {
        if ("200".equals(userBean.getStatus())) {
            String filename = AppConfig.USER_INFO;
            PreferenceHelper.write(aty, filename, AppConfig.USER_ID, userBean.getDatas().get(0).getAccountId() + "");
            PreferenceHelper.write(aty, filename, AppConfig.USER_NAME, userBean.getDatas().get(0).getFullName());
            PreferenceHelper.write(aty, filename, AppConfig.USER_DEPT, userBean.getDatas().get(0).getDepartmentName());
//            showActivity(aty, Act_Main.class);
//            finish();

            Map<String, String> params = new HashMap<>();
            params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            phonePresenter.getMenuPhone(params);
        } else {
            showActivity(aty, Act_Login.class);
            finish();
        }
    }


    @Override
    public void showUpdateInfo(UpdateBean updateBean) {
        try {
            int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
//            if (updateBean.getVersionCode() > versionCode) {
//                UpdateManager updateManager = new UpdateManager(this, updateBean, versionCode);
//                updateManager.showNoticeDialog();
//            } else {
            Boolean islogin = PreferenceHelper.readBoolean(this, AppConfig.USER_INFO, AppConfig.ISLOGIN);
            if (!islogin) {
                Timer timer = new Timer();
                TimerTask tast = new TimerTask() {
                    @Override
                    public void run() {
                        showActivity(aty, Act_Login.class);
                        finish();
                    }
                };
                timer.schedule(tast, 2000);
            } else {
                dialog = new LoadingDialog(aty);
                presenter = new LoginPresneter(new LoginModel(), this);
                Map<String, String> params = new HashMap<>();
                params.put("username", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERNAME));
                params.put("password", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERPWD));
                params.put("deviceToken", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERDEVICE));
                params.put("appKey", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERAPPKEY));
                presenter.dologin(params);
            }
//            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showUpdateError() {
        Boolean islogin = PreferenceHelper.readBoolean(this, AppConfig.USER_INFO, AppConfig.ISLOGIN);
        if (!islogin) {
            Timer timer = new Timer();
            TimerTask tast = new TimerTask() {
                @Override
                public void run() {
                    showActivity(aty, Act_Login.class);
                    finish();
                }
            };
            timer.schedule(tast, 2000);
        } else {
            dialog = new LoadingDialog(aty);
            presenter = new LoginPresneter(new LoginModel(), this);
            Map<String, String> params = new HashMap<>();
            params.put("username", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERNAME));
            params.put("password", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERPWD));
            params.put("deviceToken", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERDEVICE));
            params.put("appKey", PreferenceHelper.readString(this, AppConfig.USER_INFO, AppConfig.USERAPPKEY));
            presenter.dologin(params);
        }
    }


    @Override
    public void getMenuPhone(String s) {
        if (s.contains("全院日报"))
            AppConfig.QYRB = true;

        if (s.contains("报表查询"))
            AppConfig.BBCX = true;

        showActivity(aty, Act_Main.class);
        finish();
    }
}
